package com.kingstar.ceres.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 
import javax.sql.DataSource;
 
@Configuration
@MapperScan(basePackages = "com.kingstar.ceres.mapper.master",
    sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
  @Value("${spring.datasource.master.driver-class-name}")
  private String driverClassName;
  @Value("${spring.datasource.master.url}")
  private String url;
  @Value("${spring.datasource.master.username}")
  private String username;
  @Value("${spring.datasource.master.password}")
  private String password;
 
  @Bean(name = "masterDataSource")
  @Primary
  public DataSource masterDataSource() {
    DruidDataSource dataSource=new DruidDataSource();
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    dataSource.setDriverClassName(driverClassName);

//    HikariDataSource dataSource = new HikariDataSource();
//    dataSource.setUsername(username);
//    dataSource.setPassword(password);
//    dataSource.setJdbcUrl(url);
//    dataSource.setDriverClassName(driverClassName);
//    dataSource.setMaximumPoolSize(60);
//    dataSource.setMinimumIdle(60);
//    dataSource.setPoolName("masterDataSource");
    return dataSource;
  }

  @Autowired
  PaginationInterceptor paginationInterceptor;
 
  /**
   * master数据源
   */
  @Bean(name = "masterSqlSessionFactory")
  @Primary
  public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);

//    MybatisConfiguration configuration = new MybatisConfiguration();
////    configuration.addInterceptor(new PerformanceInterceptor());
//    configuration.setLogImpl(StdOutImpl.class);
//    bean.setConfiguration(configuration);

//    bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));

    bean.setPlugins(new Interceptor[]{paginationInterceptor});
    bean.setMapperLocations(
        new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/master/*Mapper.xml"));
    return bean.getObject();
  }
 
  @Bean(name = "masterTransactionManager")
  @Primary
  public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }
 
  @Bean(name = "masterSqlSessionTemplate")
  @Primary
  public SqlSessionTemplate testSqlSessionTemplate(
      @Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}